/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.ui.customize;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;

import lineage.ui.L1Panel;
import lineage.ui.util.L1LoadImage;
import lineage.util.Util;

/**
 * 自定义面板-外形根据图片自适应.
 * 
 * @version 2014年2月20日上午10:42:27
 * @author jrwz
 */
public class L1CustomizePanel extends L1Panel {
    private static final long serialVersionUID = 3578831609049774080L;
    private Image img;
    /** 组件外形图片路径(图片必须是png格式). */
    protected String imgURL;

    protected L1CustomizePanel() {
    }

    @Override
    protected void init(boolean flag) {
        getImage();
        super.init(flag);
    }

    // 画出背景
    @Override
    protected void paintComponent(Graphics g) {
        // 清屏
        super.paintComponent(g);
        g.drawImage(getImage(), 0, 0, this.getWidth(), this.getHeight(), this);
    }

    private Image getImage() {
        img = L1LoadImage.getInstance().getImg(imgURL);
        if (img == null) {
            Util.message("没有找到图片(必须是png图片)：" + "(" + imgURL + ")");
            return null;
        }
        MediaTracker mt = new MediaTracker(this);
        mt.addImage(img, 0);
        try {
            mt.waitForAll(); // 开始加载由此媒体跟踪器跟踪的所有图像。此方法将等待，直到所有被跟踪的图像已经完成加载。
        } catch (InterruptedException e) {
            Util.message(e.getLocalizedMessage());
        }

        _width = img.getWidth(null);
        _height = img.getHeight(null);
        return img;
    }
}
